<?php


namespace App\Repositories;


use App\Exceptions\BasicException;

class ProductGroupRepository
{
    public function getProductGroupList($page, $perPage)
    {
        $where[] = ['active', '=', 0];
        $productGroupList = app('model_product_group')->where($where)->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);
        if ($productGroupList) return $productGroupList;
        throw new BasicException(98011);
    }

    public function getProductGroupInfoList($groupId)
    {
        $where[] = ['group_id', '=', $groupId];
        $productGroupInfoList = app('model_product_group_detail')->where($where)->orderBy('id', 'desc')->get()->map(function($value){
            $detail = app('repo_reward')->getRewardDetail($value->reward_id, $value->type, $value->number);
            $value->reward_data = $detail;
            return $value;
        });
        if ($productGroupInfoList) return $productGroupInfoList;
        throw new BasicException(98011);
    }

    public function addProductGroup($productGroup, $productDetails)
    {
        if ($productGroup['id'] > 0) {
            $this->upProductGroup($productGroup, $productDetails);
            return;
        }
        $nowTime = time();
        $productGroup['add_time'] = $nowTime;
        $productGroup['status'] = 1;
        $newProductGroup = app('model_product_group')->create($productGroup);
        if ($newProductGroup) {
            $this->addProductDetail($newProductGroup, $productDetails, $nowTime);
            return;
        }

        throw new BasicException(98010);
    }

    public function upProductGroup($productGroup, $productDetails)
    {
        $where[] = ['id', '=', $productGroup['id']];
        $newProductGroup = app('model_product_group')->where($where)->first();
        if (empty($newProductGroup)) {
            throw new BasicException(98010);
        }
        $nowTime = time();
        $this->addProductDetail($newProductGroup, $productDetails, $nowTime);
    }

    public function delProductGroup($groupIds)
    {
        $ProductGroup = [];
        $ProductGroup['update_time'] = time();
        $ProductGroup['active'] = 1;
        $newProductGroup = app('model_product_group')->wherein('id', $groupIds)->update($ProductGroup);
        if (empty($newProductGroup)) {
            throw new BasicException(98010);
        }
    }

    public function addProductDetail($productGroup, $productDetails, $nowTime)
    {
        $whereDetail[]=['group_id','=',$productGroup['id']];
        app('model_product_group_detail')->where($whereDetail)->delete();
        foreach ($productDetails as $productDetail) {
            $productDetail['add_time'] = $nowTime;
            $productDetail['sort'] = $productGroup['sort'];
            $productDetail['group_id'] = $productGroup['id'];
            $newProductGroup = app('model_product_group_detail')->create($productDetail);
            if (empty($newProductGroup)) {
                throw new BasicException(98010);
            }
        }
    }

    public function updateProductGroup($id, $status)
    {
        $where[] = ['id', '=', $id];
        $ProductGroup = [];
        $ProductGroup['status'] = $status;
        $ProductGroup['update_time'] = time();
        $newProductGroup = app('model_product_group')->where($where)->update($ProductGroup);
        if ($newProductGroup) {
            return;
        }
        throw new BasicException(98010);
    }

    public function getExchangeProduct($page, $perPage, $params)
    {
        $whereArr = [];
        // 用户id
//        if (!empty($params["user_id"])) {
//            $whereArr[] = ["user_id", "=", $params["user_id"]];
//        }
        if (!empty($params["user_code"])) {
            $whereArr[] = ["user_code", "=", $params["user_code"]];
        }
        // 用户id
//        if (!empty($params["group_id"])) {
//            $whereArr[] = ["group_id", "=", $params["group_id"]];
//        }
        $scoreExchange = app('model_score_mall_exchange')
            ->where($whereArr)
            ->with([
                'user_data' => function ($query) use ($params){
                    $query->select('user_id', 'nickname', 'user_code');
                    if (!empty($params["nickname"])) {
                        $query->where('nickname', 'like', "%" . $params['nickname'] . "%");
                    }
                    if (!empty($params["user_id"])) {
                        $query->where('user_code', '=', $params['user_id']);
                    }
                },
            ])
            ->with([
                'reward_data' => function ($query) use ($params){
                    $query->select('id', 'name', 'type', 'picture');
                    if (!empty($params["name"])) {
                        $query->where('name', 'like', "%" . $params['name'] . "%");
                    }
                    if (!empty($params["group_id"])) {
                        $query->where('type', '=', $params['group_id']);
                    }
                },
            ])
            //->with('user_data:user_id,nickname', 'reward_data:id,name,type,picture')
            ->orderBy('id', 'desc')->paginate($perPage, ['*'], '', $page);
        if ($scoreExchange) {
            return $scoreExchange;
        }
        throw new BasicException(98010);
    }

}
